﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using Kyoh.Lib.Plugin;

namespace Kyoh.App.Crawler
{
    partial class PluginPreferenceControl : UserControl
    {
        public PluginPreferenceControl()
        {
            InitializeComponent();
        }

        private IPlugin _plugin;
        public IPlugin Plugin
        {
            get { return _plugin; }
            set
            {
                if (_plugin == value)
                    return;
                _plugin = value;
                if (_plugin != null)
                {
                    pluginPreferencePropertyGrid.SelectedObject = _plugin.Preferences;
                    var type = _plugin.Preferences.GetType();
                    var props = TypeDescriptor.GetProperties(pluginPreferencePropertyGrid.SelectedObject);
                }
            }
        }

        public event EventHandler OK;
        public event EventHandler Cancel;

        private void OkClickEvent(object sender, EventArgs e)
        {
            if (_plugin != null && pluginPreferencePropertyGrid.SelectedObject != null)
                _plugin.Preferences = pluginPreferencePropertyGrid.SelectedObject;
            OnOkClick(e);
        }

        protected virtual void OnOkClick(EventArgs e)
        {
            var handler = OK;
            if (handler != null)
                handler(this, e);
        }

        private void CancelClickEvent(object sender, EventArgs e)
        {
            OnCancelClick(e);
        }

        protected virtual void OnCancelClick(EventArgs e)
        {
            var handler = Cancel;
            if (handler != null)
                handler(this, e);
        }
    }
}
